{
 "metadata": {
  "signature": "sha256:f2eafc897cad6205ebbffaf5909cc79c8fdf8949ca5808696d4f927bdd4d17fc"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Matplotlib VTK integration\n",
      "======================================================================\n",
      "\n",
      "Just in case you would ever like to incorporate matplotlib plots into\n",
      "your vtk application, vtk provides a really easy way to import them.\n",
      "\n",
      "Here is a full example for now:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from vtk import *\n",
      "\n",
      "import matplotlib\n",
      "matplotlib.use('Agg')\n",
      "from matplotlib.figure import Figure\n",
      "from matplotlib.backends.backend_agg import FigureCanvasAgg\n",
      "import pylab as p\n",
      "\n",
      "# The vtkImageImporter will treat a python string as a void pointer\n",
      "importer = vtkImageImport()\n",
      "importer.SetDataScalarTypeToUnsignedChar()\n",
      "importer.SetNumberOfScalarComponents(4)\n",
      "\n",
      "# It's upside-down when loaded, so add a flip filter\n",
      "imflip = vtkImageFlip()\n",
      "imflip.SetInput(importer.GetOutput())\n",
      "imflip.SetFilteredAxis(1)\n",
      "\n",
      "# Map the plot as a texture on a cube\n",
      "cube = vtkCubeSource()\n",
      "\n",
      "cubeMapper = vtkPolyDataMapper()\n",
      "cubeMapper.SetInput(cube.GetOutput())\n",
      "\n",
      "cubeActor = vtkActor()\n",
      "cubeActor.SetMapper(cubeMapper)\n",
      "\n",
      "# Create a texture based off of the image\n",
      "cubeTexture = vtkTexture()\n",
      "cubeTexture.InterpolateOn()\n",
      "cubeTexture.SetInput(imflip.GetOutput())\n",
      "cubeActor.SetTexture(cubeTexture)\n",
      "\n",
      "ren = vtkRenderer()\n",
      "ren.AddActor(cubeActor)\n",
      "\n",
      "renWin = vtkRenderWindow()\n",
      "renWin.AddRenderer(ren)\n",
      "\n",
      "iren = vtkRenderWindowInteractor()\n",
      "iren.SetRenderWindow(renWin)\n",
      "\n",
      "# Now create our plot\n",
      "fig = Figure()\n",
      "canvas = FigureCanvasAgg(fig)\n",
      "ax = fig.add_subplot(111)\n",
      "ax.grid(True)\n",
      "ax.set_xlabel('Hello from VTK!', size=16)\n",
      "ax.bar(xrange(10), p.rand(10))\n",
      "\n",
      "# Powers of 2 image to be clean\n",
      "w,h = 1024, 1024\n",
      "dpi = canvas.figure.get_dpi()\n",
      "fig.set_figsize_inches(w / dpi, h / dpi)\n",
      "canvas.draw() # force a draw\n",
      "\n",
      "# This is where we tell the image importer about the mpl image\n",
      "extent = (0, w - 1, 0, h - 1, 0, 0)\n",
      "importer.SetWholeExtent(extent)\n",
      "importer.SetDataExtent(extent)\n",
      "importer.SetImportVoidPointer(canvas.buffer_rgba(0,0), 1)\n",
      "importer.Update()\n",
      "\n",
      "iren.Initialize()\n",
      "iren.Start()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "To have the plot be a billboard:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "bbmap = vtkImageMapper()\n",
      "bbmap.SetColorWindow(255.5)\n",
      "bbmap.SetColorLevel(127.5)\n",
      "bbmap.SetInput(imflip.GetOutput())\n",
      "\n",
      "bbact = vtkActor2D()\n",
      "bbact.SetMapper(hmap)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Comments\n",
      "--------"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "From zunzun Fri Aug 19 07:06:44 -0500 2005\n",
      "From: zunzun\n",
      "Date: Fri, 19 Aug 2005 07:06:44 -0500\n",
      "Subject: \n",
      "Message-ID: <20050819070644-0500@www.scipy.org>\n",
      "\n",
      "from http://sourceforge.net/mailarchive/forum.php?thread_id=7884469&forum_id=33405\n",
      "\n",
      "If pylab is imported before vtk, everything works fine:\n",
      " \n",
      "import pylab, vtkpython\n",
      "pylab.ylabel(\"Frequency\\n\", multialignment=\"center\", rotation=90)\n",
      "n, bins, patches = pylab.hist([1,1,1,2,2,3,4,5,5,5,8,8,8,8], 5)\n",
      "pylab.show()\n",
      " \n",
      "If however vtk is imported first:\n",
      " \n",
      "import vtkpython, pylab\n",
      "pylab.ylabel(\"Frequency\\n\", multialignment=\"center\", rotation=90)\n",
      "n, bins, patches = pylab.hist([1,1,1,2,2,3,4,5,5,5,8,8,8,8], 5)\n",
      "pylab.show()\n",
      " \n",
      "then the Y axis label is positioned incorrectly on the plots."
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "From earthman Tue Oct 25 15:21:14 -0500 2005\n",
      "From: earthman\n",
      "Date: Tue, 25 Oct 2005 15:21:14 -0500\n",
      "Subject: \n",
      "Message-ID: <20051025152114-0500@www.scipy.org>\n",
      "\n",
      "The reason for this is that vtk comes with it's own freetype library, and this is the one being used if vtk is loaded first. Worse symptoms could be errors about fonts not being found. This is typically solved by importing vtk after other packages which might use freetype (pylab, wxPython, etc)."
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "From mroublic Tue Jan 10 11:26:45 -0600 2006\n",
      "From: mroublic\n",
      "Date: Tue, 10 Jan 2006 11:26:45 -0600\n",
      "Subject: One more change I had to make\n",
      "Message-ID: <20060110112645-0600@www.scipy.org>\n",
      "In-reply-to: <20050819070644-0500@www.scipy.org>\n",
      "\n",
      "When I first tried this, I had the error:\n",
      "\n",
      "Traceback (most recent call last):\n",
      "  File \"MatplotlibToVTK.py\", line 61, in ?\n",
      "    importer.SetImportVoidPointer(canvas.buffer_rgba(), 1)\n",
      "TypeError: buffer_rgba() takes exactly 3 arguments (1 given)\n",
      "\n",
      "I had to add 0,0 to the import line:\n",
      " importer.SetImportVoidPointer(canvas.buffer_rgba(0,0), 1)\n",
      "\n",
      "I'm using VTK from CVS using the 5_0 Branch from around November 2005"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "\n",
      "The above code didn't run on my system. I had to change the following line:\n",
      "fig.set_figsize_inches(w / dpi, h / dpi)\n",
      "into:\n",
      "fig.set_figsize_inches(1.0*w / dpi, 1.0*h / dpi)\n"
     ]
    }
   ],
   "metadata": {}
  }
 ]
}